<?php
//------- logistic wxapp -------
namespace app\logi\controller;

use app\logi\model\LogiPriceModel;
use cmf\controller\AdminBaseController;
use think\Db;

/**
 * Class AdminPriceController 标签管理控制器
 * @package app\logi\controller
 */
class AdminPriceController extends AdminBaseController {
    /**
     * 订单标签管理
     * @adminMenu(
     *     'name'   => '订单标签',
     *     'parent' => 'logi/AdminIndex/default',
     *     'display'=> true,
     *     'hasView'=> true,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '订单标签',
     *     'param'  => ''
     * )
     */
    public function index() {
        $logiPriceModel = new logiPriceModel();
        $tags           = $logiPriceModel->paginate();

        $this->assign("arrStatus", $logiPriceModel::$STATUS);
        $this->assign("tags", $tags);
        $this->assign('page', $tags->render());
        return $this->fetch();
    }

    /**
     * 添加订单标签
     * @adminMenu(
     *     'name'   => '添加订单标签',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> true,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '添加订单标签',
     *     'param'  => ''
     * )
     */
    public function add() {
        $logiPriceModel = new logiPriceModel();
        $this->assign("arrStatus", $logiPriceModel::$STATUS);
        return $this->fetch();
    }

    /**
     * 添加订单标签提交
     * @adminMenu(
     *     'name'   => '添加订单标签提交',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> false,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '添加订单标签提交',
     *     'param'  => ''
     * )
     */
    public function addPost() {
        $arrData = $this->request->param();

        $logiPriceModel = new logiPriceModel();
        $logiPriceModel->isUpdate(false)->allowField(true)->save($arrData);

        $this->success(lang("SAVE_SUCCESS"),url('AdminPrice/index'));

    }

    /**
     * 修改订单标签
     * @adminMenu(
     *     'name'   => '修改订单标签',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> true,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '修改订单标签',
     *     'param'  => ''
     * )
     */
    public function edit() {
        $id = $this->request->param('id', 0, 'intval');
        if ($id > 0) {
            $logiPriceModel = new logiPriceModel();
            $res = $logiPriceModel::get($id)->toArray();
            $this->assign($res);

            $this->assign("arrStatus", $logiPriceModel::$STATUS);

            return $this->fetch();
        } else {
            $this->error('操作错误!');
        }
    }

    /**
     * 修改订单标签提交
     * @adminMenu(
     *     'name'   => '修改订单标签提交',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> false,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '添加订单标签提交',
     *     'param'  => ''
     * )
     */
    public function editPost() {
        $arrData = $this->request->param();

        $logiPriceModel = new logiPriceModel();
        $logiPriceModel->isUpdate(true)->allowField(true)->save($arrData);

        $this->success(lang("SAVE_SUCCESS"),url('AdminPrice/index'));

    }

    /**
     * 更新订单标签状态
     * @adminMenu(
     *     'name'   => '更新标签状态',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> false,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '更新标签状态',
     *     'param'  => ''
     * )
     */
    public function upStatus() {
        $intId     = $this->request->param("id");
        $intStatus = $this->request->param("status");
        $intStatus = $intStatus ? 1 : 0;
        if (empty($intId)) {
            $this->error(lang("NO_ID"));
        }

        $logiPriceModel = new logiPriceModel();
        $logiPriceModel->isUpdate(true)->save(["status" => $intStatus], ["id" => $intId]);

        $this->success(lang("SAVE_SUCCESS"));

    }

    /**
     * 删除订单标签
     * @adminMenu(
     *     'name'   => '删除订单标签',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> false,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '删除订单标签',
     *     'param'  => ''
     * )
     */
    public function delete() {
        $intId = $this->request->param("id", 0, 'intval');

        if (empty($intId)) {
            $this->error(lang("NO_ID"));
        }
        $logiPriceModel = new logiPriceModel();

        $logiPriceModel->where(['id' => $intId])->delete();
        Db::name('logi_tag_post')->where('tag_id', $intId)->delete();
        $this->success(lang("DELETE_SUCCESS"));
    }
}
